package com.qzdsoft.erpcloud.service.proj.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.qzdsoft.erpcloud.domain.proj.MaterialInfo;
import com.qzdsoft.erpcloud.domain.proj.MaterialInfoKey;
import com.qzdsoft.erpcloud.domain.proj.MaterialReview;
import com.qzdsoft.erpcloud.domain.sys.LoginUser;
import com.qzdsoft.erpcloud.domain.sys.User;
import com.qzdsoft.erpcloud.mapper.proj.MaterialInfoMapper;
import com.qzdsoft.erpcloud.mapper.proj.MaterialReviewMapper;
import com.qzdsoft.erpcloud.mapper.proj.MaterialReviewQueryMapper;
import com.qzdsoft.erpcloud.service.proj.MaterialReviewService;
import com.qzdsoft.erpcloud.vo.proj.MaterialReviewInfo;
import com.qzdsoft.erpcloud.vo.proj.MaterialReviewQueryInfo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;

/**
 * Author:ZhangHao
 * Date:2017-08-16
 * 功能：物料审核 实现
 */
@Service
public class  MaterialReviewServiceImpl implements MaterialReviewService {
	@Autowired
	private MaterialReviewMapper reviewMapper;
	@Autowired
	private MaterialReviewQueryMapper reviewViMapper;
	@Autowired
    private MaterialInfoMapper materialInfoMapper;
	
	/**
	 * 查询所有物料审核信息
	 * @see com.qzdsoft.erpcloud.service.proj.MaterialReviewService#findPage(int, int)
	 */
	@Override
    public LayTableResponse<MaterialReviewInfo> findPage(int page, int limit){
	    PageHelper.startPage(page, limit);
        List<MaterialReviewInfo> reviewInfo = reviewViMapper.findAllReview();
        LayTableResponse<MaterialReviewInfo> review = new LayTableResponse<MaterialReviewInfo>(reviewInfo);
        return review;
    }
	/**
	 * 条件查询
	 * @see com.qzdsoft.erpcloud.service.proj.MaterialReviewService#findPageByItems(com.qzdsoft.erpcloud.vo.proj.MaterialReviewQueryInfo)
	 */
	@Override
    public LayTableResponse<MaterialReviewInfo> findPageByItems(MaterialReviewQueryInfo queries){
	    PageHelper.startPage(queries.getPage(), queries.getLimit());
        List<MaterialReviewInfo> materialreview = reviewViMapper.findPageByItems(queries);
        LayTableResponse<MaterialReviewInfo> reviewRes = new LayTableResponse<MaterialReviewInfo>(materialreview);
        return reviewRes;
    }
	
	//新增物料审核
    @Override
	public Response<String> addMaterialReview(MaterialReview review, LoginUser loginUser) throws Exception {
			//判断Form 提交是否为空
	    	if(review==null){
	    		throw new Exception("提交数据为空，不可保存！！");
	    	}
	    	//设置公司ID
	    	review.setCompanyId(1);
	    	//设置创建人ID
	    	review.setCreatorId(1);
	    	//设置创建时间
	    	review.setCtime(new Date());
	    	//设置审核状态
	    	review.setReviewStatus(false);
	    	reviewMapper.insert(review);
		    return Response.success("新增物料审核成功");
	}
	/**
	 * 根据id 查询唯一条数据
	 * @throws Exception 
	 */
	@Override
	public MaterialReviewInfo getReviewViById(LoginUser loginUser, int id) throws Exception {
		MaterialReview searchVi= this.reviewMapper.selectByPrimaryKey(id);
		if(searchVi==null){
			throw new Exception("未找到相关数据！！");
		}
		MaterialReviewInfo _searchVi= this.reviewViMapper.selectByPrimaryKey(id);
		return _searchVi;
	}
	//编辑保存方法
    @Override
	public Response<String> editReview(MaterialReview review, LoginUser loginUser) {
		MaterialReview verifyEn = this.reviewMapper.selectByPrimaryKey(review.getId());
		if(verifyEn==null){
			return  Response.error("物料审核信息修改失败，当前记录未找到！！");
		}
		MaterialInfoKey key = new MaterialInfoKey();
		key.setId(review.getMaterialId());
		MaterialInfo material = this.materialInfoMapper.selectByPrimaryKey(key);
		
		verifyEn.setModifyId(1);
		verifyEn.setModifyTime(new Date());
		verifyEn.setIsNoUsed(review.getIsNoUsed());
		verifyEn.setIsSafied(review.getIsSafied());
		verifyEn.setMaterialCoding(material.getMaterialCoding());
		verifyEn.setMaterialId(review.getMaterialId());
		verifyEn.setRemark(review.getRemark());
		verifyEn.setSafetyCertificateEnd(review.getSafetyCertificateEnd());
		verifyEn.setSafetyCertificateStart(review.getSafetyCertificateStart());
		verifyEn.setSupplierId(review.getSupplierId());
		int id = this.reviewMapper.updateByPrimaryKey(verifyEn);
		MaterialReviewInfo returnEn = this.reviewViMapper.selectByPrimaryKey(id);
		return Response.success("物料审核信息修改成功");
	}
	//删除方法
    @Override
	public Response<String> delById(Integer id, LoginUser loginUser) {
		MaterialReview searchEn= this.reviewMapper.selectByPrimaryKey(id);
		if(searchEn==null){
			return  Response.error("删除失败，当前记录未找到！！");
		}
		this.reviewMapper.deleteByPrimaryKey(id);
		return Response.success("物料审核信息删除成功");
	}
	//审核记录
    @Override
	public Response<String> audtById(Integer id, LoginUser loginUser) {
		MaterialReview searchEn= this.reviewMapper.selectByPrimaryKey(id);
		if(searchEn==null){
			return  Response.error("审核失败，当前记录未找到！！");
		}
		searchEn.setReviewId(1);
		searchEn.setReviewStatus(true);
		searchEn.setReviewTime(new Date());
		this.reviewMapper.updateByPrimaryKey(searchEn);
		return Response.success("物料审核信息审核成功");
	}
	//弃审记录
    @Override
	public Response<String> unAudtById(Integer id, LoginUser loginUser) {
		MaterialReview searchEn= this.reviewMapper.selectByPrimaryKey(id);
		if(searchEn==null){
			return  Response.error("弃审失败，当前记录未找到！！");
		}
		searchEn.setReviewId(null);
		searchEn.setReviewStatus(false);
		searchEn.setReviewTime(null);
		this.reviewMapper.updateByPrimaryKey(searchEn);
		return Response.success("物料审核信息弃审成功");
	}
}